package com.krui.iweibo.ui;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.krui.iweibo.R;
import com.krui.iweibo.adapter.UserInfoAdapter;
import com.krui.iweibo.model.Task;
import com.krui.iweibo.model.UserInfo;
import com.krui.iweibo.service.MainService;
import com.krui.iweibo.utils.SharedPreferenceUtil;

/**
 * LoginActivity.java
 * 
 * @author krui
 * 2012-6-26 上午07:42:03
 * TODO
 * 
 */
public class LoginActivity extends BaseActivity implements IweiboActivity
{
	
	private Button btnLogin;
	private List<UserInfo> users;
	private Button btn_user_selecter;
	private ImageView imge_user_head;
	private TextView  txt_login_show_name,tv_title;
	
	private ProgressDialog pDialog;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		//启动服务
		Intent service = new Intent(this, MainService.class);
		startService(service);
		//把自己放到Activity集合里面
		MainService.addActivity(this);
		
		init();
		
	}

	
	public void init()
	{
		//从数据库中查询所有用户
		users = dbAccess.getAllUsers();
		if (users == null || users.isEmpty()) {
			//跳转到授权页面进行授权
			intent2Where(LoginActivity.this,AuthActivity.class, true);
			
		}

		initView();
	}
	
	
	/**
	 * 初始化view
	 */
	private void initView(){
		imge_user_head = (ImageView) this.findViewById(R.id.imge_user_head);
		txt_login_show_name = (TextView) this.findViewById(R.id.txt_login_show_name);
		
		tv_title = (TextView)findViewById(R.id.tv_title);
		tv_title.setText(getString(R.string.weibologin));
		
		
		//currentUser = SharedPreferenceUtil.getLoginUser(this);
		currentUser = users.get(0);
		
		if (currentUser != null) {
			//如果获取到当前用户不为空，设置头像信息并调用登陆
			imge_user_head.setImageBitmap(currentUser.getUserIcon());
			txt_login_show_name.setText(currentUser.getUserName());
		//	showDialog();
		//	loginTask();
		}
		
		btn_user_selecter = (Button)findViewById(R.id.btn_user_selected);
		btn_user_selecter.setOnClickListener(new UserSelectOnClickListener());
		
		//点击登录
		btnLogin = (Button) this.findViewById(R.id.btn_login);
		btnLogin.setOnClickListener(new OnClickListener()
		{
			
			public void onClick(View v)
			{
				if (currentUser == null) {
					showHintDialog(getString(R.string.user_null));
					return;
				}
				showDialog();
				//保存登陆用户
				SharedPreferenceUtil.savaLoginUser(LoginActivity.this, currentUser);
				
				loginTask();
			}
		});
		
	}
	
	



	/**
	 * 登陆
	 */
	private void loginTask(){
		
		Map<String , Object> taskParams = new HashMap<String, Object>();
		taskParams.put("loginUser", currentUser);
		Task task = new Task(Task.WEIBO_LOGIN, taskParams);
		MainService.newTask(task);
		MainService.addActivity(LoginActivity.this);
	}
	
	
	
	public void refresh(Object... params)
	{
		
		pDialog.dismiss();
		
		currentUser = (UserInfo)params[0];
		
		long loginTime = System.currentTimeMillis();
		
		dbAccess.updateUserLoginTime(currentUser.getUserId(), loginTime +  "");
		
		SharedPreferenceUtil.savaLoginUser(this, currentUser);
		//跳转到主界面
		//intent2Where(LoginActivity.this, MainActivity.class,true);
		Intent mainIntent = new Intent(LoginActivity.this,MainActivity.class);
		startActivity(mainIntent);
		this.finish();
		
		
	}
	
	
	/**
	 * 选择登录用户监听器
	 * LoginActivity.java
	 * @author krui
	 * 2012-6-27 上午10:07:58
	 *
	 */
	final class UserSelectOnClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			View viewDlg = View.inflate(LoginActivity.this, R.layout.user_selected_dialog, null);
			
			final Dialog dialog = new Dialog(LoginActivity.this, R.style.user_selected_dialog);
			
			dialog.setContentView(viewDlg);
			
			dialog.show();
			
			
			ListView listView = (ListView)viewDlg.findViewById(R.id.lv_user_list);
			UserInfoAdapter adapter = new UserInfoAdapter(LoginActivity.this, users);
			listView.setAdapter(adapter);
			
			listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					ImageView ivUserIcon = (ImageView)view.findViewById(R.id.img_user_head_temp);
					TextView tvUserName = (TextView)view.findViewById(R.id.txt_show_name);
					//得到bitmap头像
					ivUserIcon.setDrawingCacheEnabled(true);
					Bitmap bmp = Bitmap.createBitmap(ivUserIcon.getDrawingCache());
					ivUserIcon.setDrawingCacheEnabled(false);
					//换成圆角bitmap
					imge_user_head.setImageBitmap(bmp);
					txt_login_show_name.setText(tvUserName.getText());
					
					//用户选择登录后，从数据库中查出这个用户作为当前登录用户
					currentUser = dbAccess.getUserById(id+"", null);
					
					dialog.dismiss();
					
				}
			});
			
			
		}
		
	}
	
	
	/**
	 * 显示进度框
	 */
	private void showDialog(){
		if (pDialog == null) {
			pDialog = new ProgressDialog(this);
			//pDialog.setCancelable(false);
			pDialog.setMessage(getString(R.string.logining));
			pDialog.show();
		}
		
	}
	

	
}
